require("./index.css");
var _mm = require("util/mm.js");

var indexTemplate = require("./index.string");

var simplePagination = function(){
	var _this = this;
	this.defaultOption = {
		container: null,
		pageNum: 1,
		onSelectPage: null
	};
	// 事件的处理
	$(document).on('click', '.pg-item', function() {
		var $this = $(this);
		// 对于active和disabled按钮点击，不做处理
		if($this.hasClass('disabled')) {
			return;
		}
		typeof _this.option.onSelectPage === 'function' ?
			_this.option.onSelectPage($this.data('value'), $this) : null;
	});
};

simplePagination.prototype.render = function(userOption) {
	// 合并选项
	this.option = $.extend({}, this.defaultOption, userOption);
	// 判断容器是否为合法的jquery对象
	if(!(this.option.container instanceof jQuery)) {
		return;
	}
	// 判断是否只有1页
	if(this.option.pages <= 1) {
		this.option.container.html("");
		return;
	}
	// 渲染分页内容
	this.option.container.html(this.getHtml());
};

simplePagination.prototype.getHtml = function() {
	var option = this.option;
	var pageArray = [];
	pageArray.push({
		value: option.prePage,
		disabled: !option.hasPreviousPage,
		name: "上一页",
		prefix: option.prefix
	});
	pageArray.push({
		value: option.nextPage,
		disabled: !option.hasNextPage,
		name: "下一页",
		prefix: option.prefix
	});
	return _mm.renderHtml(indexTemplate, {
		pageArray: pageArray
	});
}

module.exports = simplePagination;